import store from '../store'
import {
	existParm,
	addParm,
	getParm
} from '@/util/stringUtil.js';
export default {
	routeHandler: function(e) {
   
		if (!e || !e.targetType) {
			uni.switchTab({
				url: '../index/index'
			})
		} else {
			switch (e.targetType) {
				case 0: //无事件
					break;
				case 1: //其他小程序
					uni.navigateToMiniProgram({
						appId: e.targetOtherAppId,
						path: e.targetOtherPath
					})
					break;
				case 2: //内部链接
					var path = decodeURIComponent(e.targetPath);
					if (!path)
						path = '/pages/index/index';

					if (!(path.startsWith('https') || path.startsWith('http') || path.startsWith("/")))

						path = '/' + path;

					path = this.replaceUrl(path);
					switch (path) {
						case '/pages/index/index':
						case '/pages/category/category':
						case '/pages/user_center/user_center':
							uni.switchTab({
								url: path
							})
							break;
						default:
							// #ifdef H5

							if (path.indexOf('pages/web/web') > -1 || path.indexOf('pages/web_view/web_view') > -1) {
								var _href = getParm(path, 'target');
								console.log(_href)
								_href = decodeURIComponent(_href);
								console.log(_href)
								if (_href) {
									window.location.href = _href;
								}
							}
							if (path.startsWith('https') || path.startsWith('http')) {
								window.location.href = path;
							}
							// #endif
							uni.navigateTo({
								url: path
							})
							break;
					}
					break;
				case 3: //网页链接
					var path = decodeURIComponent(e.targetUrl);
					if (!path)
						uni.switchTab({
							url: '/pages/index/index'
						})
					// #ifdef H5
					window.location.href = path;
					// #endif
					// #ifndef H5
					uni.navigateTo({
						url: '/pages/web_view/web_view?target=' + encodeURIComponent(path)
					})
					// #endif
					break;
				case 4: //打开图片
					var path = decodeURIComponent(e.targetSrc);
					uni.previewImage({
						urls: [path]
					})
					break
				case 7:
					if(e.targetMessage){
						uni.showModal({
							content: e.targetMessage,
							showCancel: false,
							confirmText: '我知道了'
						})
					}
					break;
				default:
					break;
			}
		}
	},
	returnUrlHandler: function(url) {
	
		var _url = decodeURIComponent(url);
	
		var _urlArr = _url.split(store.state.common.hostDomain);
     
		if (_urlArr.length >1) {
			_url = _urlArr[1];
		}
		
		if (_url == store.state.common.hostDomain) {
			_url = '';
		}
 
		if (_url && _url.startsWith('?'))
			_url = '';
		if (!_url)
			_url = 'pages/index/index';
  
		switch (_url) {
			case 'pages/index/index':
			case 'pages/category/category':
			case 'pages/study_center/study_center':
			case 'pages/user_center/user_center':
				uni.switchTab({
					url: `/${_url}`
				})
				break;
			default:
				// #ifdef H5
				if (_url.indexOf('pages/web/web') > -1 || _url.indexOf('pages/web_view/web_view') > -1) {
					var _href = getParm(_url, 'target');
					
					if (_href) {
						window.location.href = _href;
					}
				}
				break;
				// #endif
				_url = this.replaceUrl(_url);
				if(!_url.startsWith('/')) _url='/'+_url;
				uni.redirectTo({
					url: _url
				})
				break;
		}
	},
	reloadUrlHandler: function() {

		// #ifdef H5
		var isIOS = uni.getSystemInfoSync().platform == 'ios';
		if (isIOS) {
			var _h5Url = window.location.href;
			var _urlArr = _h5Url.split(store.state.common.hostDomain);
			if (_urlArr.length > 1) {
				_h5Url = _urlArr[1];
			}
			if (!existParm(_h5Url, "reLoad")) {
				_h5Url = addParm(_h5Url, 'reLoad', 'true');
				window.location.replace(store.state.common.hostDomain + _h5Url);

			}
		}
		// #endif
	},
	replaceUrl: (url) => {

		if (url.indexOf('/pages/album/audio/audio') > -1)
			url = url.replace('/pages/album/audio/audio', '/pages/album/audio');
		else if (url.indexOf('/pages/album/video/video') > -1)
			url = url.replace('/pages/album/video/video', '/pages/album/video');
		else if (url.indexOf('/pages/album/poem/list/list') > -1) {
			url = url.replace('/pages/album/poem/list/list', '/pages/album/audio');
			url = url + '&hasText=true';
		} else if (url.indexOf('/pages/goods/goods1/detail') > -1) {
			url = url.replace('/pages/goods/vip_goods');
		} else if (url.indexOf('/pages/index/mine/bindPhone/bindPhone') > -1) {
			url = url.replace('/pages/index/mine/bindPhone/bindPhone', '/pages/user_center/bindPhone');
		} else if (url.indexOf('/pages/order/list/orderList') > -1) {
			url = url.replace('/pages/order/list/orderList', '/pages/goods/order_list')
		} else if (url.indexOf('/pages/manage/volunteer/add/add') > -1) {
			url = url.replace('/pages/manage/volunteer/add/add', '/pages/community/volunteer/add');
		} else if (url.indexOf('/pages/channel/mine/mine') > -1) {
			url = url.replace('/pages/channel/mine/mine', '/pages/channel/mine');
		} else if (url.indexOf('/pages/channel/user/user') > -1) {
			url = url.replace('/pages/channel/user/user', '/pages/channel/user')
		} else if (url.indexOf('/pages/channel/orderList/log') > -1) {
			url = url.replace('/pages/channel/orderList/log', '/pages/channel/order')
		} else if (url.indexOf('/pages/agency/list/list') > -1) {
			url = url.replace('/pages/agency/list/list', '/pages/agency/mine')
		} else if (url.indexOf('/pages/agency/user/user') > -1) {
			url = url.replace('/pages/agency/user/user', '/pages/agency/user')
		} else if (url.indexOf('/pages/agency/stockLog/log') > -1) {
			url = url.replace('/pages/agency/stockLog/log', '/pages/agency/stockLog');
		} else if (url.indexOf('/pages/agency/orderList/log') > -1) {
			url = url.replace('/pages/agency/orderList/log', '/pages/agency/order');
		} else if (url.indexOf('/pages/agency/orderSum/log') > -1) {
			url = url.replace('/pages/agency/orderSum/log', '/pages/agency/orderSum');
		} else if (url.indexOf('/pages/agency/charge/charge') > -1) {
			url = url.replace('/pages/agency/charge/charge', '/pages/agency/charge');
		} else if (url.indexOf('/pages/agency/addressManage/list/addr') > -1) {
			url = url.replace('/pages/agency/addressManage/list/addr', '/pages/agency/addressList');
		} else if (url.indexOf('/pages/agency/delivery/delivery') > -1) {
			url = url.replace('/pages/agency/delivery/delivery', '/pages/agency/delivery')
		} else if (url.indexOf('/pages/channel/ExchangePreUser/ExchangePreUser') > -1) {
			url = url.replace('/pages/channel/ExchangePreUser/ExchangePreUser', '/pages/channel/exchange_pre_user');
		} else if (url.indexOf('/pages/channel/changeList/ChangeList') > -1) {
			url = url.replace('/pages/channel/changeList/ChangeList', '/pages/channel/change_list');
		} else if (url.indexOf('/pages/manage/list/list') > -1) {
			url = url.replace('/pages/manage/list/list', '/pages/community/list');
		} else if (url.indexOf('/pages/manage/center/center') > -1) {
			url = url.replace('/pages/manage/center/center', '/pages/community/center');
		} else if (url.indexOf('/pages/manage/managers/managers/managers') > -1) {
			url = url.replace('/pages/manage/managers/managers/managers', '/pages/community/manager/list')
		} else if (url.indexOf('/pages/manage/students/students/students') > -1) {
			url = url.replace('/pages/manage/students/students/students', '/pages/community/students/list')
		} else if (url.indexOf('/pages/manage/zone/add/add') > -1) {
			url = url.replace('/pages/manage/zone/add/add', '/pages/community/zone/add');
		} else if (url.indexOf('/pages/web/web') > -1) {
			url = url.replace('/pages/web/web', '/pages/web_view/web_view')
		}
		return url;
	}
}
